aliroot $1 -b -l <<EOF

  tree=$2;
printf("Tree: %s\n", "$2");

//========================

  TList *ll=new TList;
  gStyle->SetOptStat("enoum");

  tree->SetScanField(0);

  //---
  c1=new TCanvas("c1");
  TH2D *hmpv=new TH2D("hmpv","", 65, 6539, 6604,200,0,20);
  tree->Draw("mpv:ibin>>hmpv", "mpv>1e-12");
  c1->cd(); gPad->SetGrid(1,0); hmpv->GetYaxis()->SetRangeUser(0,6); hmpv->Draw("colz");ll->Add(hmpv); ll->Add(c1);

  //---
  c2=new TCanvas("c2");
  TH2D *hp0=new TH2D("hp0","", 65, 6539, 6604,200,0,20);
  tree->Draw("1/p0:ibin>>hp0", "p0>1e-12");
  c2->cd(); gPad->SetGrid(1,0); hp0->GetYaxis()->SetRangeUser(0,6); hp0->Draw("colz");ll->Add(hp0); ll->Add(c2);

  //---
  c3=new TCanvas("c3");
  TH2D *hnor=new TH2D("hnor","", 65, 6539, 6604,1000,0,10000);
  tree->Draw("nor:ibin>>hnor", "nor>1e-12");
  c3->cd(); gPad->SetGrid(1,0); hnor->GetYaxis()->SetRangeUser(0,2000); hnor->Draw("colz"); ll->Add(hnor); ll->Add(c3);

  //=======================

//should have 540*31=16740 entries
tree->Scan("nor:mpv:p0:run:AliTRDdEdxUtils::ToSector(ibin):AliTRDdEdxUtils::ToStack(ibin):AliTRDdEdxUtils::ToLayer(ibin):AliTRDdEdxUtils::ToTimeBin(ibin)"); > scanalltb.log

//should be the same as from checkOBJ
tree->Scan("nor:mpv:p0:run:AliTRDdEdxUtils::ToSector(ibin):AliTRDdEdxUtils::ToStack(ibin):AliTRDdEdxUtils::ToLayer(ibin):AliTRDdEdxUtils::ToTimeBin(ibin)", "p0>1e-12"); > scancalib.log

//reason for gmpv=0:      
//1)gnor<=3; truncation in hist: (0, 0.6*ntot=1.8 with ntot=3]={1}, in hist entries can pile up so that ntot=2, or 3, and (ntot>nstart && ntot<=nstop) is skipped;
//2)TruncatedMean(hist) out of range.
tree->Scan("nor:mpv:wid:p0:run:AliTRDdEdxUtils::ToSector(ibin):AliTRDdEdxUtils::ToStack(ibin):AliTRDdEdxUtils::ToLayer(ibin):AliTRDdEdxUtils::ToTimeBin(ibin):ibin+1","mpv<1e-12 && nor>3"); > scanNofilledtb.log

//should be none
tree->Scan("mpv:p0:run:nor:res:AliTRDdEdxUtils::ToSector(ibin):AliTRDdEdxUtils::ToStack(ibin):AliTRDdEdxUtils::ToLayer(ibin):AliTRDdEdxUtils::ToTimeBin(ibin)","mpv>1e-12 && p0<1e-12"); > scanNoCalibtb.log

//=======================

TFile *fout=new TFile("drawCalibStream.root","recreate");
  fout->cd();
  ll->Write();
  fout->Save();
  fout->Close();


.q
EOF

for ii in $(echo scanalltb scancalib scanNofilledtb scanNoCalibtb)
do
    echo $ii
    cat $ii.log | grep -v Row | grep -v "\*\*\*" | grep entr -v | wc 
done

rm -f scanalltb.log
